package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.Result;
import com.example.domain.dto.RoleDTO;
import com.example.domain.entity.Role;
import com.example.domain.query.RoleQuery;
import com.example.domain.vo.MenuPermissionVO;
import com.example.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "角色管理")
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @Operation(summary = "获取角色列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:query')")
    public Result<IPage<Role>> list(RoleQuery query) {
        return Result.success(roleService.getRolePage(query));
    }

    @Operation(summary = "获取所有角色")
    @GetMapping("/all")
    public Result<List<Role>> getAllRoles() {
        return Result.success(roleService.list(new LambdaQueryWrapper<Role>()
                .eq(Role::getDeleted, 0)
                .orderByAsc(Role::getSort)));
    }

    @Operation(summary = "获取角色详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:query')")
    public Result<Role> getInfo(@PathVariable Long id) {
        return Result.success(roleService.getRoleById(id));
    }

    @Operation(summary = "新增角色")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:role:add')")
    public com.example.common.Result<?> add(@RequestBody @Valid RoleDTO roleDTO) {
        roleService.addRole(roleDTO);
        return Result.success();
    }

    @Operation(summary = "修改角色")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:role:update')")
    public Result<?> update(@RequestBody @Valid RoleDTO roleDTO) {
        roleService.updateRole(roleDTO);
        return Result.success();
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result<?> delete(@PathVariable Long id) {
        roleService.deleteRole(id);
        return Result.success();
    }

    @Operation(summary = "批量删除角色")
    @DeleteMapping("/batch")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result<?> batchDelete(@RequestBody List<Long> ids) {
        roleService.batchDeleteRole(ids);
        return Result.success();
    }

    @Operation(summary = "获取角色菜单")
    @GetMapping("/{roleId}/menus")
    @PreAuthorize("hasAuthority('sys:role:query')")
    public Result<List<Long>> getRoleMenus(@PathVariable Long roleId) {
        return Result.success(roleService.getRoleMenuIds(roleId));
    }

    @Operation(summary = "分配角色菜单")
    @PostMapping("/{roleId}/menus")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public Result<?> assignMenus(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        roleService.assignMenus(roleId, menuIds);
        return Result.success();
    }

    @Operation(summary = "获取角色菜单权限")
    @GetMapping("/{roleId}/menu-permissions")
    @PreAuthorize("hasAuthority('sys:role:query')")
    public Result<List<MenuPermissionVO>> getRoleMenuPermissions(@PathVariable Long roleId) {
        return Result.success(roleService.getRoleMenuPermissions(roleId));
    }
} 